package org.zs.tool;

import java.io.InputStream;
import java.util.Properties;


public class ConfigTool {
	
	private Properties p;
	private InputStream in;
	private static ConfigTool config = new ConfigTool();
	private ConfigTool(){
		p = new Properties();
		in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
		
		try {
			p.load(in);
		} catch (Exception e) {
			System.out.println("error:=====加载配置文件出错(请检查类路径下的config.properties)");
		}
	}
	
	public static ConfigTool getTool()
	{
		return config;
	}
	
	public String getURL()
	{
		String tmp = p.getProperty("URL");
		if(tmp==null)
		{
			System.out.println("error:=====获取config.properties的数据库URL失败");
		}
		return tmp;
	}
	
	public String getUser()
	{
		String tmp = p.getProperty("USER");
		if(tmp==null)
		{
			System.out.println("error:=====获取config.properties的数据库账户失败");
		}
		return tmp;
	}
	
	public String getPassword()
	{
		String tmp = p.getProperty("PASSWORD");
		if(tmp==null)
		{
			System.out.println("error:=====获取config.properties的数据库密码失败");
		}
		return tmp;
	}
	
	public String get(String key)
	{
		return p.getProperty(key);
	}
	
	public boolean debug()
	{
		String tmp = p.getProperty("DEBUG");
		if(tmp==null)
		{
			System.out.println("info:====获取不到config.properties文件中的debug选项");
			return true;
		}
		if(tmp.equals("true"))
			return true;
		if(tmp.equals("false"))
			return false;
		System.out.println("info:====config.properties文件中的debug选项只能是false或true");
		return true;
	}
	
}
